<?php
class core_statistics extends object {

public $uid="";
public $timefrom="";
public $timeto="";

function core_statistics($from,$to) {
	$this->uid=$_SESSION['uid'];
	if ($this->validate_time_format($from) == "") {
		$this->timefrom=strtotime($from." 00:00:01");
		} else {$this->timefrom="";}
	if ($this->validate_time_format($to) == "") {
		$this->timeto=strtotime($to. " 23:59:59");
		} else {$this->timeto="";}
}

function validate_time_format($string) {
$loc = new loc;
		$out="";
 if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $date, $parts)) {
	if(checkdate ($parts[1] ,$parts[2] ,$parts[3] )) {
		} else {
		$out=$out."<br>".$loc->time_date_missmatch;
		}
 	}
return $out;
}

function validate_values() {
	$loc = new loc;
	$out="";
	if ($this->timefrom == "" OR $this->timeto == "") {
		$out=$out."<br>".$loc->missing_startstoptime;
		}
	return $out;	
	}

function return_total() {
	
	$validation1=$this->validate_values();
	if ($validation == "") {
	$query="select sum(duration) as total from userjobs where from_ >= ".$this->timefrom." AND to_ <= ".$this->timeto." AND uid = ".$this->uid."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	//return $query;
	return $row['total'];
	}
}
}

class core_statistics_advanced extends core_statistics {

function core_statistics_advanced($from,$to,$uid) {
	$this->uid=$uid;
	if ($this->validate_time_format($from) == "") {
		$this->timefrom=strtotime($from);
		} else {$this->timefrom="";}
	if ($this->validate_time_format($to) == "") {
		$this->timeto=strtotime($to);
		} else {$this->timeto="";}
}

//compute time for single user on all statistically valid jobs
function user_on_jobs($uid) {
$total=0;
$query2="select ID from tree where statistics = 1";
$result2=mysql_query($query2);
while ($row2=mysql_fetch_array($result2)) {
	$query="select sum(duration) as total from userjobs where from_ > ".$this->timefrom." AND to_ <".$this->timeto." AND jid=".$row2['ID']." AND uid=".$uid."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	$total=$total+$row['total'];
	
}
return $total;	
//	return $query;
}

//compute job hours (including subjobs) user spend on particular job in time period
function job_on_user($jid,$uid) {
	//$queryX="";
	$total=0;
	$query="select * from tree where ID=".$jid."";
	$result=mysql_query($query);
	$jobstats=mysql_fetch_array($result);
	$query2="select * from tree where left_state >= ".$jobstats['left_state']." AND right_state <= ".$jobstats['right_state']." AND statistics = 1";
	$result2=mysql_query($query2);
	while ($row2=mysql_fetch_array($result2)) {
		$query3="select sum(duration) as total from userjobs where from_ > ".$this->timefrom." AND to_ <".$this->timeto." AND jid=".$row2['ID']." AND uid=".$uid."";
		$result3=mysql_query($query3);
		$row3=mysql_fetch_array($result3);
		$total=$total+$row3['total'];
		//$queryX=$queryX."<br>".$query3;
	}
return $total;
//return $queryX;
}

//summary jobtime for allusers in timeperiod
function job_on_users($jid) {
	$total=0;
	$query="select id from users where statistics = 1";
	$result=mysql_query($query);
	while ($row=mysql_fetch_array($result)) {
		$total=$total+$this->job_on_user($jid,$row['id']);
	}
return $total;
}

function return_real_time_allusers($jid) {
	$query="select sum(duration) as total from userjobs WHERE uid in (select id from users where statistics=1) AND jid=".mysql_real_escape_string($jid)." AND from_ > ".mysql_real_escape_string($this->timefrom)." AND to_ < ".mysql_real_escape_string($this->timeto)." ";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	return $row['total'];
	//return $query;
}

function return_real_time($jid,$uid) {
	$query="select sum(duration) as total from userjobs WHERE uid = ".mysql_real_escape_string($uid)." AND jid=".mysql_real_escape_string($jid)." AND from_ > ".mysql_real_escape_string($this->timefrom)." AND to_ < ".mysql_real_escape_string($this->timeto)." ";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	return $row['total'];

}














}
